"""
Support module for logAnalyzer.py to calculate the packet statistics.

Calculates statistics for the given packet list.
"""


from support import packet

class packetStatistics:
	"""
	Class that calculates the statistics for the given packet list
	
	Keyword arguments:
    packetList -- list of packets to analyze
	"""
	def __init__(self, packetList):
		"""Default Constructor"""
		self.packetList = packetList
		
	def __str__(self):
		"""Default String Output"""
		return ""
					
	def getNumbers(self):
		""" Gets the number of packets on each direction ( IN and OUT ) """
		packetsIn = 0
		packetsOut = 0
		errors = 0
		for x in self.packetList:
			if x.direction == 'IN':
				packetsIn += 1
			elif x.direction == 'OUT':
				packetsOut += 1
			else:
				errors += 1
		return packetsIn, packetsOut, errors

	def getCountryStats(self):
		""" returns the number of packets by country"""
		output = {}
		for x in self.packetList:
			try:
				output[x.country] += 1
			except(KeyError):
				output[x.country] = 1
		return output
		
	def getCityStats(self):
		"""returns the number of packets by city"""
		output = {}
		for x in self.packetList:
			try:
				output[x.city] = (output[x.city][0]+1,output[x.city][1],output[x.city][2])
			except(KeyError):
				output[x.city] = (1,x.longitude,x.latitude)
		return output
		
	def getIPStats(self):
		"""returns the number of packets by IP"""
		output = {}
		for x in self.packetList:
			try:
				output[x.IP] += 1
			except(KeyError):
				output[x.IP] = 1
		return output
		
	def getMACStats(self):
		"""returns the number of packets by MAC address"""
		output = {}
		mac = ''
		for x in self.packetList:
			if x.direction == 'IN':
				mac = x.MACIN
			else:
				mac = x.MACOUT
			try:
				output[mac] += 1
			except(KeyError):
				output[mac] = 1
		return output
		
	def getPacketsByDate(self):
		"""returns Packet data by Date ( Day and Month )"""
		output = {}
		"""returns the number of packets by IP"""
		output = {}
		for x in self.packetList:
			try:
				output[(str(x.timestamp[0]) + ' ' + str(x.timestamp[1]) )] += 1
			except(KeyError):
				output[(str(x.timestamp[0]) + ' ' + str(x.timestamp[1]) )] = 1
		return output
